package org.hilo.boot.core;

import org.jretty.apibase.IStatus;

/**
 * The outcome of a result.
 *
 * @author zollty
 * @since 2020年11月10日
 */
public enum Status implements IStatus {

    /**
     * Outcome of the request was success.
     */
    SUCCESS(200),

    /**
     * Outcome of the request was client error.
     */
    CLIENT_ERROR(400),

    /**
     * Outcome of the request was server error.
     */
    SERVER_ERROR(500),

    /**
     * Outcome of the request was unknown.
     */
    UNKNOWN(600);
    
    private int status;
    
    private Status(int status) {
        this.status = status;
    }

    /**
     * Return the {@code Outcome} for the given HTTP {@code status} code.
     * @param status the HTTP status code
     * @return the matching Outcome
     */
    public static Status forCode(String code) {
        if (code == null || code.length() == 0 || "0".equals(code) || "200".equals(code)) {
            return SUCCESS;
        }
        return forErrorCode(code);
    }
    
    public static Status forErrorCode(String code) {
        if (code.startsWith("C")) {
            return CLIENT_ERROR;
        }
        if (code.startsWith("S")) {
            return SERVER_ERROR;
        }
        return UNKNOWN;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

}
